<?php
/**
 * Created by lj.
 * User: lj
 * Date: 2021/9/16
 * Time: 1:20 下午
 */

namespace PhpLibs\TPLDesign;


//use Endroid\QrCode\Color\Color;
//use Endroid\QrCode\Encoding\Encoding;
//use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
//use Endroid\QrCode\QrCode;
//use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;

class Scene
{
    public $data = '{"background":{"uri":"http://ncwu.jijiaox.com/static/tpl/enroll.jpeg","width":600,"height":400},"objectList":[{"key":"name","name":"姓名","x":206,"y":161,"font":"隶书","fontSize":20,"color":"#000","status":0},{"key":"name","name":"姓名","x":211,"y":137,"font":"隶书","fontSize":20,"color":"#000","status":0},{"key":"sex","name":"性别","x":423,"y":144,"font":"隶书","fontSize":20,"color":"#000","status":0},{"key":"birthday","name":"出生年月","x":232,"y":211,"font":"隶书","fontSize":20,"color":"#000","status":0}],"metaList":[{"key":"name","name":"姓名","status":0},{"key":"sex","name":"性别","status":0},{"key":"birthday","name":"出生年月","status":0},{"key":"major","name":"专业","status":0},{"key":"year","name":"年级","status":0}]}';

    public function __construct()
    {

         $this->fontPath = [
            'lishu' => __DIR__.'/font/SIMLI.TTF',
            'kaiti' => __DIR__.'/font/SIMKAI.TTF'
        ];
    }

    public function makeImage($data, $tpl){
        $template = $this->parseTmplate($tpl);

        $image = $this->loadBackground($template['background']);
        imagesavealpha($image, true);
        $textcolor = imagecolorallocatealpha($image, 0, 0, 0, 0);

        //循环赋值
        $objectList = $template['objectList'];
        foreach ($objectList as $obj){
            $key = $obj['key'];
            $x = $obj['x'];
            $y = $obj['y'];
            $fontSize = $obj['fontSize'];
            $font = $obj['font'];
            $color = $obj['color'];
            $width = 0;

            if ($font == '隶书'){
                $fontPath = $this->fontPath['lishu'];
            }else {
                $fontPath = $this->fontPath['kaiti'];
            }

            if(isset($data[$key])){
                imagettftext($image, $fontSize, 0, $x+$width, $y, $textcolor, $fontPath, $data[$key]);
            }
        }

        $path = '';
        imagepng($image, $path);
        imagedestroy($image);

        return $path;
    }


    public function setTpl($tpl){
        $this->template = $this->parseTmplate($tpl);
    }

    public function make($data){

    }

    private function parseTmplate($tmpl){
        $tmplate = json_decode($tmpl, true);
        return $tmplate;
    }

    private function loadBackground($background){
        $uri = $background['uri'];
        $width = $background['width'];
        $height = $background['height'];

        if(strpos($uri,'/') !== 0){
            $data = file_get_contents($uri);
            $file = fopen('', 'w+');
            fwrite($file, $data);
            fclose($file);
        }else{
            $fileName = $uri;
        }


        $extion = pathinfo($uri, PATHINFO_EXTENSION);
        switch ($extion){
            case 'png':
                $image = imagecreatefrompng($fileName);
                break;
            case 'jpg':
            case 'jpeg':
                $image = imagecreatefromjpeg($fileName);
                break;
            default:
                throw new \Exception('不支持的类型,仅支持PNG,JPEG格式图片');
        }
        //如果大小和图片大小不一致，需要对图片进行放缩

        return $image;
    }


    private function test(){
        /*
        if ($fileName) {
            $path = $dir . $fileName;
        } else {
            $fileName = date('Ymd', time()) . DS . $student->name . '_' . time() . '.png';
            $path = $dir . $fileName;
        }
        if (file_exists($path)) {
            unlink($path);
        }
        if (!file_exists(pathinfo($path, PATHINFO_DIRNAME))) {
            mkdir(pathinfo($path, PATHINFO_DIRNAME),0755,true);
        }
        $fontPath = ROOT_PATH.'public/static/font/SIMLI.TTF';
        //copy($templatePath, $path);

        $url = 'http://ncwu.jijiaox.com/mh/find.html';

        $image = imagecreatefromjpeg($templatePath);
        imagesavealpha($image, true);
        $textcolor = imagecolorallocatealpha($image, 0, 0, 0, 0);
        //imagecolortransparent($image, $textcolor);
        $fontSize = 26*2;

        //生成qrcode
        $writer = new \Endroid\QrCode\Writer\PngWriter();
        // Create QR code
        $qrCode = QrCode::create($url)
            ->setEncoding(new Encoding('UTF-8'))
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
            ->setSize(300)
            ->setMargin(10)
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));
        $result = $writer->write($qrCode);
        $result->saveToFile($path.'_qrcode.png');
        $qr = imagecreatefrompng($path.'_qrcode.png');
        $x = 714;
        $y = 645;
        @imagecopyresized($image,$qr, $x,$y,0,0, 325,325,300,300);
        @imagedestroy($qr);
        @unlink($path.'_qrcode.png');
        */
    }
}